/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cuong.bean;

import client.entity.Category;
import client.entity.Customer;
import client.entity.JobSubject;
import client.entity.LevelJob;
import client.entity.Location;
import client.entity.Recruitment;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;

/**
 *
 * @author Admin Windows 8
 */
@ManagedBean
@RequestScoped
public class SearchJobByLocale {

     private static final int DEFAULT_RECORDS_NUMBER = 10;
    private static final int DEFAULT_PAGE_INDEX = 1;
    private int records;
    private int recordsTotal;
    private int pageIndex;
    private int pages;
    private List<JobSubject> list;
    private List<JobSubject> model;
    private Map<String, String> params;
    private int idLocale;
      private List<Recruitment> listRecruiment;
    
    public SearchJobByLocale() {
        
        this.records = DEFAULT_RECORDS_NUMBER;
        this.pageIndex = DEFAULT_PAGE_INDEX;  
        this.recordsTotal = getList().size();
        if (records > 0) {
            pages = records <= 0 ? 1 : recordsTotal / records;
            if (recordsTotal % records > 0) {
                pages++;
            }
            if (pages == 0) {
                pages = 1;
            }
        } else {
            records = 1;
            pages = 1;
        }
        updateList();
    }
    
 public void updateList() {
        int fromIndex = getFirst();
        int toIndex = getFirst() + records;
        if (toIndex > this.recordsTotal) {
            toIndex = this.recordsTotal;
        }
        this.model = list.subList(fromIndex, toIndex);
    }

    public void next() {
        if (this.pageIndex < pages) {
            this.pageIndex++;
        }

        updateList();
    }

    public void prev() {
        if (this.pageIndex > 1) {
            this.pageIndex--;
        }
        updateList();
    }

    public void first() {

        this.pageIndex = 1;

        updateList();
    }

    public void last() {
        this.pageIndex = pages;
        updateList();
    }

    public int getRecords() {
        return records;
    }

    public int getRecordsTotal() {
        return recordsTotal;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public int getPages() {
        return pages;
    }

    public int getFirst() {
        return (pageIndex * records) - records;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }
       public List<JobSubject> getModel() {
        return model;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public void setParams(Map<String, String> params) {
        this.params = params;
    }

    public void setList(List<JobSubject> list) {
        this.list = list;
    }

  

    public List<JobSubject> getList() {
      list = new ArrayList<JobSubject>();
        List<Recruitment> listRe = getListRecruiment();
        for (Recruitment recruitment : listRe) {
            JobSubject js = new JobSubject();
            js.setId(recruitment.getId());
            js.setTitle(recruitment.getTitle());
            if (recruitment.getTypeOfJob() == true) {
                js.setTypejob("Full Time");
            } else {
                js.setTypejob("Pass Time");
            }
            js.setNamecustomer(getCustomerByID(recruitment.getCustomer().getId()).getName());
            js.setNameleveljob(getLevelJobByID(recruitment.getLevelJob().getId()).getName());

            List<Category> listCate = getListCateByIDRecrui(recruitment.getId());
            StringBuffer stringCate = new StringBuffer();
            for (int i = 0; i < listCate.size(); i++) {
                Category ct = listCate.get(i);
                if (i < listCate.size() - 1) {
                    stringCate.append(ct.getCategoryName() + ", ");
                } else {
                    stringCate.append(ct.getCategoryName());
                }
            }
            js.setNamecate(stringCate.toString());

            List<Location> listLoca = getListLocaByIDRecrui(recruitment.getId());
            StringBuffer stringLocale = new StringBuffer();
            for (int i = 0; i < listLoca.size(); i++) {
                Location lc = listLoca.get(i);
                if (i < listLoca.size() - 1) {
                    stringLocale.append(lc.getName() + ", ");
                } else {
                    stringLocale.append(lc.getName());
                }
            }
            js.setNamelocation(stringLocale.toString());
            list.add(js);
        }
        return list;
    }
     

      public List<Recruitment> getListRecruiment() {
        String prIDlc = getParam("idlocale");
        if (prIDlc == null) {
            idLocale = (Integer) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("idlocale");
        } else {
            idLocale=Integer.parseInt(prIDlc);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("idlocale", idLocale);
        }
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();           
        listRecruiment = ss.createQuery("select re from Recruitment as re inner join re.recruitmentLocations as rl where rl.location.id="+idLocale).list();
        ss.getTransaction().commit();
        ss.close();
        return listRecruiment;
    }

    public List<Category> getListCateByIDRecrui(int idRecrui) {
        List<Category> list = new ArrayList<Category>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select c from Category as c inner join c.recruitmentCategories as rc where rc.recruitment.id=" + idRecrui).list();
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public List<Location> getListLocaByIDRecrui(int idRecrui) {
        List<Location> list = new ArrayList<Location>();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        list = ss.createQuery("select lo from Location as lo inner join lo.recruitmentLocations as rl where rl.recruitment.id=" + idRecrui).list();
        ss.getTransaction().commit();
        ss.close();
        return list;
    }

    public LevelJob getLevelJobByID(int id) {
        LevelJob lj = new LevelJob();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        lj = (LevelJob) ss.createQuery("from LevelJob lj where lj.id=" + id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    }

    public Customer getCustomerByID(int id) {
        Customer lj = new Customer();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session ss = factory.openSession();
        ss.getTransaction().begin();
        lj = (Customer) ss.createQuery("from Customer cus where cus.id=" + id).uniqueResult();
        ss.getTransaction().commit();
        ss.close();
        return lj;
    }
    public String getParam(String param) {
        params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        return params.get(param);
    }
}
